package LeetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dijah
 * @Date: 2022/12/30 13:18
 * @Desc: 数组的子集
 * 输入：nums = [1,2,3]
 * 输出：[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
 */
public class code78_subsets {

    static List<List<Integer>> res = new ArrayList<>();

    public static List<List<Integer>> subsets(int [] nums){
        backtrack(0, nums, res, new ArrayList<Integer>());
        return res;
    }

    public static void backtrack(int i,int [] nums,List<List<Integer>> res,ArrayList<Integer> tmp){
        res.add(new ArrayList<>(tmp));
        for (int j = i; j < nums.length; j++) {
            tmp.add(nums[j]);
            backtrack(j + 1, nums, res, tmp);
            tmp.remove(tmp.size() - 1);
        }
    }

    public static void main(String[] args) {
        int [] nums = {1,2,3};
        List<List<Integer>> subsets = subsets(nums);
        for (List<Integer> subset : subsets) {
            System.out.println(subset.toString());
        }
    }
}

//
//    private void backtrack(int i, int[] nums, List<List<Integer>> res, ArrayList<Integer> tmp) {
//        res.add(new ArrayList<>(tmp));
//        for (int j = i; j < nums.length; j++) {
//            tmp.add(nums[j]);
//            backtrack(j + 1, nums, res, tmp);
//            tmp.remove(tmp.size() - 1);
//        }
//    }
